package com.xiaoshuidi.cloud.module.member.controller.admin.point;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.excel.core.util.ExcelUtils;
import com.xiaoshuidi.cloud.module.member.controller.admin.point.vo.*;
import com.xiaoshuidi.cloud.module.member.controller.app.point.vo.PointsForm;
import com.xiaoshuidi.cloud.module.member.controller.app.point.vo.TenantPointsRecordVo;
import com.xiaoshuidi.cloud.module.member.controller.app.point.vo.TenantPointsVo;
import com.xiaoshuidi.cloud.module.member.convert.point.PointsConvert;
import com.xiaoshuidi.cloud.module.member.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.member.service.point.MemberPointsRecordService;
import com.xiaoshuidi.cloud.module.member.service.point.MemberPointsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Tag(name = "管理后台 - 积分管理")
@RestController
@RequestMapping(ApiConstants.NAME+"/admin-points")
@Validated
@Slf4j
public class PointsController {

    @Resource
    MemberPointsService tenantPointsService;
    @Resource
    MemberPointsRecordService tenantPointsRecordService;

    @PostMapping("/page")
    @Operation(summary = "积分分页列表")
    public CommonResult<PageResult<MemberPointsPageRespVo>> getMemberPointsPage(@RequestBody MemberPointsPageReqVo reqVo){
        PageResult<MemberPointsPageRespVo> page = tenantPointsService.getMemberPointsPage(reqVo);
        return CommonResult.success(page);
    }

    @PostMapping("/export")
    @Operation(summary = "积分列表导出")
    public void exportTenantPoints(@RequestBody MemberPointsPageReqVo reqVo, HttpServletResponse response) throws IOException {
        List<MemberPointsPageRespVo> list = tenantPointsService.exportTenantPoints(reqVo);
        List<MemberPointsExcelVo> export = PointsConvert.INSTANCE.convertPointExport(list);
        ExcelUtils.write(response,"积分列表导出.xls", "积分列表导出", MemberPointsExcelVo.class, export);
    }

    @PostMapping("/add")
    @Operation(summary = "增加积分")
    public CommonResult<Boolean> plusTenantPoints(@RequestBody MemberPointsChangeVo reqVo){
        tenantPointsService.plusTenantPoints(reqVo);
        return CommonResult.success(Boolean.TRUE);
    }

    @PostMapping("/deduct")
    @Operation(summary = "积分扣除")
    public CommonResult<Boolean> minusTenantPoints(@RequestBody MemberPointsChangeVo reqVo){
        tenantPointsService.minusTenantPoints(reqVo);
        return CommonResult.success(Boolean.TRUE);
    }

    @PostMapping("/change/page")
    @Operation(summary = "积分变动分页列表")
    public CommonResult<PageResult<MemberPointChangesPageRespVo>> getPointsChangePage(@RequestBody MemberPointChangesPageReqVo reqVo){
        PageResult<MemberPointChangesPageRespVo> page = tenantPointsRecordService.getPointsChangePage(reqVo);
        return CommonResult.success(page);
    }

    @GetMapping("/change/export")
    @Operation(summary = "积分变动列表导出")
    public void exportPointsChangeList(MemberPointChangesPageReqVo reqVo, HttpServletResponse response) throws IOException {
        List<MemberPointChangesExportVo> list = tenantPointsRecordService.exportPointsChangeList(reqVo);
        ExcelUtils.write(response,"积分变动导出.xls", "积分变动表", MemberPointChangesExportVo.class, list);
    }

    @PostMapping("/records")
    @Operation(summary = "个人客户 - 积分明细")
    public CommonResult<PageResult<TenantPointsRecordVo>> getTenantPointRecords(@RequestBody PointsForm form){
        PageResult<TenantPointsRecordVo> page = tenantPointsRecordService.getTenantPointsRecords(form);
        return CommonResult.success(page);
    }

    @PostMapping("/summary")
    @Operation(summary = "个人客户 - 积分汇总")
    public CommonResult<TenantPointsVo> getTenantPointsSummary(@RequestBody PointsForm form){
        TenantPointsVo vo = tenantPointsService.getByTenantUserId(form.getTenantUserId());
        return CommonResult.success(vo);
    }
}
